﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Data.SqlClient;


[DataContract]
class Marker_result {
    [DataMember(Order = 0)]
    public IList<Marker_Marker> markers { get; set; }
}

/// <summary>
/// 返回結果
/// </summary>
[DataContract]
class Marker_Marker
{
    [DataMember(Order = 0)]
    public string id { get; set; }
    [DataMember(Order = 1)]
    public string name { get; set; }
    [DataMember(Order = 2)]
    public string city { get; set; }
    [DataMember(Order = 3)]
    public string area { get; set; }
    [DataMember(Order = 4)]
    public string address { get; set; }
    [DataMember(Order = 5)]
    public string phone { get; set; }
    [DataMember(Order = 6)]
    public string desc { get; set; }
    [DataMember(Order = 7)]
    public string lat { get; set; }
    [DataMember(Order = 8)]
    public string lng { get; set; }
    [DataMember(Order = 9)]
    public string recommend { get; set; }
    [DataMember(Order = 10)]
    public string starpoint { get; set; }
    [DataMember(Order = 11)]
    public string datasource { get; set; }
    [DataMember(Order = 12)]
    public string class_id { get; set; }
    [DataMember(Order = 13)]
    public string class_name { get; set; }
    [DataMember(Order = 14)]
    public IList<Marker_Photo> photo { get; set; }
}
[DataContract]
class Marker_Photo
{
    [DataMember(Order = 0)]
    public string url { get; set; }
    [DataMember(Order = 1)]
    public string sort { get; set; }
    [DataMember(Order = 2)]
    public string originalContextUrl { get; set; }
}

public partial class Service_Marker : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["mkid"]))
        {
            string[] Markers = Request.QueryString["mkid"].Split(',');
            Marker_result result = new Marker_result();
            result.markers = new List<Marker_Marker>();

            using (MyDb.Db db = new MyDb.Db())
            {
                foreach (string marker in Markers)
                {
                    Marker_Marker item = new Marker_Marker();
                    //獲取當前公車位置
                    using (SqlDataReader rs = db.getList(string.Format("select a.*,b.NameZh as ClassName from MarkerInfo a left join MarkerClass b on b.ID=a.MarkerClassID where a.ID='{0}'", marker)))
                    {
                        if (rs.Read())
                        {
                            item.id = rs["ID"].ToString().Trim();
                            item.name = rs["NameZh"].ToString().Trim();
                            item.city = rs["City"].ToString().Trim();
                            item.area = rs["Area"].ToString().Trim();
                            item.address = rs["Address"].ToString().Trim();
                            item.phone = rs["Phone"].ToString().Trim();
                            item.lat = rs["Lat"].ToString().Trim();
                            item.lng = rs["Lng"].ToString().Trim();
                            item.desc = rs["Desc"].ToString().Trim();
                            item.recommend = rs["recommend"].ToString().Trim();
                            item.starpoint = rs["starpoint"].ToString().Trim();
                            item.datasource = rs["datasource"].ToString().Trim();
                            item.class_id = rs["MarkerClassID"].ToString().Trim();
                            item.class_name = rs["ClassName"].ToString().Trim();
                            item.photo = new List<Marker_Photo>();
                            /*獲取地標*/
                            using (DataTable dt2 = db.getDataTable(string.Format("select * from MarkerPhoto where MarkerID='{0}'", rs["ID"]), "mess2"))
                            {
                                foreach (DataRow rs2 in dt2.Rows)
                                {
                                    Marker_Photo item2 = new Marker_Photo();
                                    item2.url = rs2["URL"].ToString().Trim();
                                    item2.originalContextUrl = rs2["originalContextUrl"].ToString().Trim();
                                    item2.sort = rs2["sort"].ToString().Trim();
                                    item.photo.Add(item2);
                                }
                            }
                            result.markers.Add(item);
                            item = null;

                        }
                        rs.Close();
                    }
                }
            }
            Response.Write(string.Format("{0}", MyDb.JSON.stringify(result)));
        }
        Response.End();
    }
}
